from py2neo import Graph


class neo4jdb():
    url = "mongodb://localhost:27017/"
    user='neo4j'
    psw='s3cr3t'
    graph=None
    pass

    @classmethod
    def getGraph(cls,dbname):

        if neo4jdb.graph is  None:
            neo4jdb.graph = Graph("http://localhost:7474", username="neo4j", password='password')



        return  neo4jdb.graph
    @classmethod
    def deletAll(cls,dbname):
        if neo4jdb.graph is None:
            neo4jdb.getGraph(dbname)
        neo4jdb.graph.delete_all()

    @classmethod
    def getAllLabels(cls,dbname):
        if neo4jdb.graph is None:
            neo4jdb.getGraph(dbname)
        return  neo4jdb.graph.schema.nodelabels

    @classmethod
    def getAllRelationsTypes(cls, dbname):
        if neo4jdb.graph is None:
            neo4jdb.getGraph(dbname)
        return neo4jdb.graph.schema.relationship_types

    @classmethod
    def txStart(cls, dbname):
        if neo4jdb.graph is None:
            neo4jdb.getGraph(dbname)
            neo4jdb.graph.begin()

    @classmethod
    def txEnd(cls, dbname):
        neo4jdb.graph.end()

    @classmethod
    def queryByCphql(cls, dbname,cqlStr):
        if neo4jdb.graph is None:
            neo4jdb.getGraph(dbname)
        neo4jdb.graph.run(cqlStr)
    @classmethod
    def save(cls, baseNode):
        a = Node('PersonTest', name='张三')
        b = Node('PersonTest', name='李四')
        r = Relationship(a, 'KNOWNS', b)
        s = a | b | r
        graph.create(s)


